import 'package:flutter/material.dart';
import '../sites/biquge.dart';

class BookSearchList extends StatefulWidget {
  @override
  _BookSearchListState createState() => _BookSearchListState();
}

class _BookSearchListState extends State<BookSearchList> {
  TextEditingController searchController = TextEditingController();
  Future _future;
  @override
  void initState() {
    super.initState();
    this._future = BiqugeSpider.search(this.searchController.text);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        actions: <Widget>[
          Expanded(
            flex: 2,
            child: Container(),
          ),
          Expanded(
            flex: 10,
            child: TextField(
              controller: searchController,
              cursorColor: Colors.black,
              decoration: InputDecoration(
                hintText: '请输入书名或作者',
              ),
              autofocus: true,
              onSubmitted: (String searchContent) async {
                setState(() {
                  this._future = BiqugeSpider.search(searchController.text);
                });
              },
            ),
          ),
          Expanded(
            flex: 2,
            child: GestureDetector(
                onTap: () async {
                  FocusScope.of(context).requestFocus(FocusNode());
                  setState(() {
                    this._future = BiqugeSpider.search(searchController.text);
                  });
                },
                child: Container(
                  margin: EdgeInsets.only(right: 16),
                  child: Icon(Icons.search, size: 30),
                )),
          )
        ],
      ),
      body: searchController.text.isEmpty
          ? Center(child: Text("暂无内容~"))
          : FutureBuilder(
              // future: BiqugeSpider.search(searchController.text),
              future: this._future,
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                if (snapshot.connectionState != ConnectionState.done) {
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                }
                if (snapshot.data.isEmpty) {
                  return Center(
                    child: Text("没查到结果哦~"),
                  );
                }
                List searchResult = snapshot.data;
                return ListView.builder(
                  itemCount: searchResult.length,
                  itemBuilder: (BuildContext context, int index) {
                    Map current = searchResult[index];
                    return ListTile(
                      onTap: () {
                        Navigator.pushNamed(context, "/book/detail",
                            arguments: {
                              "url": current["bookUrl"],
                              "title": current["bookTitle"]
                            });
                      },
                      leading: Image.network(current["bookCoverUrl"]),
                      title: Text(current["bookTitle"]),
                      subtitle: Text(
                          "${current['author']} | ${current['latestChapter']}"),
                    );
                  },
                );
              },
            ),
    );
  }
}
// 西游记  校花
